﻿using Easy4net.Common;
using Easy4net.DBUtility;
using MyPlan.Models;
using MyPlan.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace MyPlan.Controllers
{
    public class CompanyController : ApiController
    {
        [HttpGet]
        public Response Get(int id)
        {
            Response response = Response.NewResponse();

            DBHelper db = DBHelper.getInstance();
            Company company = db.FindById<Company>(id);

            return response.Ok(company);
        }

        [HttpGet]
        public Response FindByPage(int page, int limit)
        {
            Response response = Response.NewResponse();

            DBHelper db = DBHelper.getInstance();

            string strSQL = "SELECT * FROM company";
            ParamMap param = ParamMap.newMap();
            param.setPageIndex(page);
            param.setPageSize(limit);
            param.setOrderFields("created", true);
            List<Company> companys = db.FindBySql<Company>(strSQL, param);

            return response.Ok(companys);
        }

        [HttpPost]
        public Response Add([FromBody]Company company)
        {
            Response response = Response.NewResponse();
            company.Created = DateTime.Now;

            DBHelper db = DBHelper.getInstance();
            db.Save<Company>(company);

            return response.Ok(company);
        }
    }
}
